package models;

import java.util.List;

import siena.Column;
import siena.Id;
import siena.Index;
import siena.Model;
import siena.Query;
import util.oauthclient.ICredentials;

public class Usuario extends Model implements ICredentials {
	@Id
	public Long oid;

	@Column("identifier")
	@Index("identifier_index")
	String identifier;

	@Column("login")
	@Index("login_index")
	String login;

	@Column("nome")
	String nome;

	@Column("secret")
	String secret;

	@Column("token")
	String token;

	@Column("originator")
	String originator;

	@Column("from")
	String from;

	public static final Usuario USUARIO_ANONIMO = new Usuario("", "",
			"Visitante") {
		public boolean isAnonimo() {
			return true;
		};
	};

	public static final String USUARIO_REF = Usuario.class.getName();

	public String getIdentifier() {
		return identifier;
	}

	public void setIdentifier(String identifier) {
		this.identifier = identifier;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String loginname) {
		this.login = loginname;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public Usuario(String identifier, String loginname, String nome) {
		this();
		this.identifier = identifier;
		this.login = loginname;
		this.nome = nome;
	}

	public Usuario() {
		this.token = this.secret = "";
	}

	public boolean isAnonimo() {
		return false;
	}

	public static Query<Usuario> all() {
		return Model.all(Usuario.class);
	}

	public static Usuario findByIdentifier(String identifier) {
		Query<Usuario> queryUsuario = all().filter("identifier", identifier);
		List<Usuario> result = queryUsuario.fetch(1);

		if (1 != result.size())
			return null;

		return result.get(0);
	}

	public static Usuario findByLogin(String login) {
		Query<Usuario> queryUsuario = all().filter("login", login);
		List<Usuario> result = queryUsuario.fetch(1);

		if (1 != result.size())
			return null;

		return result.get(0);
	}

	public static Usuario findByOriginator(String originator) {
		Query<Usuario> queryUsuario = all().filter("originator", originator);
		List<Usuario> result = queryUsuario.fetch(1);

		if (1 != result.size())
			return null;

		return result.get(0);
	}


	public static Usuario findByFrom(String from) {
		Query<Usuario> queryUsuario = all().filter("from", from);
		List<Usuario> result = queryUsuario.fetch(1);

		if (1 != result.size())
			return null;

		return result.get(0);
	}

	public String getSecret() {
		return secret;
	}

	public String getToken() {
		return token;
	}

	public void setSecret(String secret) {
		this.secret = secret;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public String getOriginator() {
		return originator;
	}

	public void setOriginator(String originator) {
		this.originator = originator;
	}

	public String getFrom() {
		return from;
	}

	public void setFrom(String from) {
		this.from = from;
	}

}
